﻿using Masa.BuildingBlocks.Data;
using Masa.CqrsProject.Models;
using Microsoft.EntityFrameworkCore;

namespace Masa.CqrsProject.Contexts
{

    /// <summary>
    /// 连接读库的数据库上下文
    /// </summary>
    [ConnectionStringName("DefaultReadConnection")]
    public class CqrsReadDbContext : MasaDbContext<CqrsReadDbContext>
    {
        /// <summary>
        /// 配置数据库上下文
        /// </summary>
        /// <param name="options"></param>
        public CqrsReadDbContext(MasaDbContextOptions<CqrsReadDbContext> options) : base(options)
        {

        }
        /// <summary>
        /// 配置数据库模型
        /// </summary>
        /// <param name="modelBuilder"></param>
        protected override void OnModelCreatingExecuting(ModelBuilder modelBuilder)
        {

            //将价格值对象映射到数据库
            modelBuilder.Entity<OrderInfo>(entity =>
            {
                entity.OwnsOne(oo => oo.OrderPrice, orderprice =>
                {
                    orderprice.Property(op => op.Price).HasColumnName("Price");
                    orderprice.Property(op => op.Currency).HasColumnName("Currency");
                });
            });
        }
        /// <summary>
        /// 订单信息
        /// </summary>
        public DbSet<OrderInfo> OrderInfo { get; set; }
        /// <summary>
        /// 订单商品
        /// </summary>
        public DbSet<OrderItems> OrderItems { get; set; }
    }
}
